//
//  ForgetVC.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/7.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class ForgetVC: MeBaseViewController {
    
    //手机号
    @IBOutlet weak var tfNum: UITextField!

    //验证码
    @IBOutlet weak var tfAuto: UITextField!
    
    //密码
    @IBOutlet weak var tfPass: UITextField!
    
    //确认密码
    @IBOutlet weak var tfConfirmPass: UITextField!

    //发送验证码
    @IBAction func sendAuto(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("发送验证码")))")
        
        //检查验证码是否为空
        if self.tfNum.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfNumIsNull)
        } else if Encryption.checkTelNumber(self.tfNum.text) == false {
            MBManager.showBriefAlert(tfNumNotCor)
        } else if Encryption.checkTelNumber(self.tfNum.text) == true {
            
            let params = ["type" : 2,
                          "phone" : self.tfNum.text as Any] as [String : Any]
            print(params)
            
            MBManager.showBriefMessage("发送中...", in: self.view)
            manager.post(sendMsgUrl, parameters: params, success: { (task, response) in
                
                let json = JSON(response:response!)
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(json)
                
                if json["resultmsg"] == "手机号已被注册" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(numUsed)
                    return
                } else if json["resultmsg"] == "成功" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(msgSended)
                }
                
            }) { (task, error) in
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(error)
            }
            
        }
    }
    
    //修改密码
    @IBAction func changePassSEL(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("修改密码")))")
        
        
        
        if self.tfNum.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfNumIsNull)
            
        } else if self.tfAuto.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfAutoNull)
            
        } else if self.tfPass.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfPassNull)
            
        } else if self.tfConfirmPass.text?.characters.count == 0 {
            MBManager.showBriefAlert(confirPassNotNull)
            
        } else if Encryption.checkTelNumber(self.tfNum.text) == false {
            MBManager.showBriefAlert(tfNumNotCor)
            
        } else if self.tfPass.text != self.tfConfirmPass.text {
            MBManager.showBriefAlert(passTwoChekc)
            
        } else if Encryption.checkTelNumber(self.tfNum.text) == true {
            let params = ["code":self.tfAuto.text as Any,
                          "newpwd":Encryption.md5Encrypt(with: self.tfPass.text),
                          "phone":self.tfNum.text as Any] as [String : Any]
            print(params)
            
            manager.post(forgetPassUrl, parameters: params, success: { (task, response) in
                /**
                 缺少手机号参数或手机格式错误或不为11位数字
                 短信验证码错误
                 缺少验证码参数
                 手机号不存在
                 你的密码修改成功
                 触发业务流控
                 */
                
                let json = JSON(response:response!)
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(json)
                
                if json["resultmsg"] == "缺少手机号参数或手机格式错误或不为11位数字" {
                    MBManager.showBriefAlert("手机号格式不对")
                } else if json["resultmsg"] == "短信验证码错误" {
                    MBManager.showBriefAlert("短信验证码错误")
                } else if json["resultmsg"] == "触发业务流控"{
                    MBManager.showBriefAlert("发送失败,请重试")
                } else if json["resultmsg"] == "你的密码修改成功"{
                    MBManager.showBriefAlert("密码修改成功")
                }
                

            }) { (task, error) in
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(error)
                MBManager.showBriefAlert("网络异常")
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
